package com.dongdongshop.controller;

import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.util.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("login")
public class LoginController {

    @RequestMapping("show")
    public String show() {
        return "login";
    }

    
    @RequestMapping("login")
    @ResponseBody
    public Result login(String username,String password){
        //创建subject对象
        Subject subject = SecurityUtils.getSubject();
        //将表单提交的账号密码封装成shiro的token
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //执行登录逻辑
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            System.out.println("账号不存在");
            return Result.response(ResultEnum.LOGIN_USERNAME_NULL);
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
            return Result.response(ResultEnum.LOGIN_PASSWORD_ERROR);
        }
        return Result.Login();
    }

}
